package com.cskaoyan.wx.service;

import com.cskaoyan.bean.Footprint;
import com.cskaoyan.bean.FootprintExample;
import com.cskaoyan.bean.Goods;
import com.cskaoyan.mapper.FootprintMapper;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.wx.bean.vo.WxFootPrintInfoVo;
import com.cskaoyan.wx.bean.vo.WxFootPrintVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * 功能:足迹
 * 作者:chen11
 */
@Service
public class WxFootPrintServiceImpl implements  WxFootPrintService {
    @Autowired
    FootprintMapper footprintMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Override
    public WxFootPrintVo SelectFootPrint(Integer page, Integer size) {
        //todo:userid没拿到
        int userid=1;
        PageHelper.startPage(page,size);
        WxFootPrintVo wxFootPrintVo = new WxFootPrintVo();


        FootprintExample footprintExample = new FootprintExample();
        footprintExample.createCriteria().andUserIdEqualTo(userid);
        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);

        //pageNum
        PageInfo<Footprint> footprintPageInfo = new PageInfo<>(footprints);
        int pageNum = footprintPageInfo.getPageNum();
        wxFootPrintVo.setTotalPages(pageNum);

        List<WxFootPrintInfoVo> footprintList=new ArrayList<>();

        for(Footprint footprint:footprints){
            Integer goodsId = footprint.getGoodsId();
            Date addTime = footprint.getAddTime();
            Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
            WxFootPrintInfoVo wxFootPrintInfoVo = new WxFootPrintInfoVo();
            wxFootPrintInfoVo.setBrief(goods.getBrief());
            wxFootPrintInfoVo.setPicUrl(goods.getPicUrl());
            wxFootPrintInfoVo.setAddTime(addTime);
            wxFootPrintInfoVo.setGoodsId(goodsId);
            wxFootPrintInfoVo.setName(goods.getName());
            wxFootPrintInfoVo.setRetailPrice(goods.getRetailPrice());
            wxFootPrintInfoVo.setId(goods.getId());
            footprintList.add(wxFootPrintInfoVo);
        }
        wxFootPrintVo.setFootprintList(footprintList);
        return wxFootPrintVo;

    }
}
